# PhotographsScreen - This screen contains a list/grid of photographs
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

# Widgets
from frontend.gui.widgets.image_menu import ImageMenu
from frontend.gui.widgets.list_indicator import ListIndicator
from frontend.gui.widgets.image_menu_item import ImageMenuItem

from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

class PhotographsScreen(Screen):
    """
    PhotographsScreen
    
    This screen displays a grid of photographs as thumbnails. User can select
    any photograph from this grid.
    """
    
    def __init__(self, ui, frontend, data):
        """
        Initialize screen
        
        @param frontend: FrontendClient object
        @param theme: Theme object
        @param data: List [screen title string, List of Image objects]
        """
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.group = clutter.Group()
        self.images = data[1]
    
        # Screen Title (Displayed at the bottom left corner)
        screen_title = clutter.Label()
        screen_title.set_font_name(self.theme.getFont() + ' 100px')
        screen_title.set_color(clutter.Color(0xff, 0xff, 0xff, 0x11))
        screen_title.set_text(data[0])
        screen_title.set_name("screen_title")
        (screen_title_width, screen_title_height) = screen_title.get_size()
        screen_title_x = 0
        screen_title_y = ui.STAGE_HEIGHT - screen_title_height + 20
        screen_title.set_position(screen_title_x, screen_title_y)
        screen_title.show()
        self.group.add(screen_title)
        
        # Image Title (over album list)
        self.image_title = clutter.Label()
        self.image_title.set_text(" ")
        self.image_title.set_font_name(self.theme.getFont() + ' 32px')
        self.image_title.set_color(self.theme.getColor("title"))
        self.image_title.set_line_wrap(False)
        self.image_title.set_ellipsize(pango.ELLIPSIZE_END)
        self.image_title.set_position(80, 610)
        self.image_title.show()
        self.group.add(self.image_title)
        
        self.image_desc = clutter.Label()
        self.image_desc.set_text(" ")
        self.image_desc.set_font_name(self.theme.getFont() + ' 32px')
        self.image_desc.set_color(self.theme.getColor("subtitle"))
        self.image_desc.set_line_wrap(True)
        self.image_desc.set_ellipsize(pango.ELLIPSIZE_END)
        self.image_desc.set_position(80, 700)
        self.image_desc.show()
        self.group.add(self.image_desc)
        
        self._create_photo_menu()
        self._create_list_indicator()
        self._update_image_info(0)
        
        self.group.show()
        ui.getStage().add(self.group)
    
    def _create_photo_menu(self):
        """
        Create a grid menu that contains all the photographs.
        """
        self.menu = ImageMenu(self.theme)
        self.menu.set_visible_column_count(6)
        
        for image in self.images:
            texture = clutter.Texture()
            buffer = gtk.gdk.pixbuf_new_from_file(image.get_thumbnail_url())
            texture.set_pixbuf(buffer)
            item = ImageMenuItem(200, 150, texture)
            item.set_userdata(image)
            self.menu.add_actor(item)
        
        self.menu.set_position(80,140)
        self.menu.show()
        
        if self.frontend.get_configuration().show_effects():
            self.menu.set_animate(True)
        
        self.group.add(self.menu)
    
    def _update_image_info(self, number):
        """
        Update image information box.
        @param number: Number of image. This is index of the self.__images
        """
        image = self.images[number]
        name = image.get_title()
        desc = image.get_description()
        self.image_title.set_text(name)
        self.image_desc.set_text(desc)
        self.image_title.set_size(500, 32)
        self.image_desc.set_size(500, 70)
        
    def _create_list_indicator(self):
        """
        Create list indicator for albums list.
        """
        self.li = ListIndicator(200,34, self.theme, ListIndicator.HORIZONTAL)
        li_y = self.frontend.getUserInterface().STAGE_HEIGHT - self.li.get_height() - 30
        li_x = 1270 - self.li.get_width()
        self.li.set_position(li_x, 610)
        self.li.set_delimiter(" | ")
        self.li.show()
        self.li.set_maximum(self.menu.get_number_of_items())
        self.li.set_current(1)
        self.group.add(self.li)
    
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "PhotographsScreen"
    
    def handle_key_press_event(self, event):   
        """ Handle key pressed event. This can be transmitted to the widgets."""
        # LEFT KEY
        if event.keyval == clutter.keysyms.Left:
            self.menu.move(ImageMenu.LEFT)
            self._update_image_info(self.menu.get_current_postition())
            self.li.set_current(self.menu.get_current_postition() + 1)
            
        # RIGHT KEY
        elif event.keyval == clutter.keysyms.Right:
            self.menu.move(ImageMenu.RIGHT)
            self._update_image_info(self.menu.get_current_postition())
            self.li.set_current(self.menu.get_current_postition() + 1)
            
        # UP KEY
        elif event.keyval == clutter.keysyms.Up:
            self.menu.move(ImageMenu.UP)
            self._update_image_info(self.menu.get_current_postition())
            self.li.set_current(self.menu.get_current_postition() + 1)
        
        # DOWN KEY
        elif event.keyval == clutter.keysyms.Down:
            self.menu.move(ImageMenu.DOWN)
            self._update_image_info(self.menu.get_current_postition())
            self.li.set_current(self.menu.get_current_postition() + 1)
        
        # ENTER KEY  
        elif event.keyval == clutter.keysyms.Return:
            index = self.menu.get_current_postition()
            self.ui.changeScreen(self.ui.createScreen("photo", [index, self.images]), 
                                 Transition.FORWARD)